import pygame,random
from pygame.locals import *
purple = (128,0,128)  # 紫色 # 黑色
Red = (255, 0, 0)  # 红色
head = [200, 200]
snake = [[200, 200], [190, 200], [180, 200], [170, 200]]
food=[random.randrange(1,60)*10,random.randrange(1,60)*10]
length = 600  # 窗口长度
width = 600  # 窗口宽度
windows=pygame.display.set_mode((600,600))
speed = pygame.time.Clock()
class Food():
    def __init__(self):
        self.food=food
    def display(self):
        pygame.draw.rect(windows,Red,pygame.Rect(food[0],food[1],10,10))


def hit_wall(head):
    if head[0] >= length or head[0] < 0 or head[1] >= width or head[1] < 0:
        return 1
    else:
        return 0

def hit_snake():
    if snake[0] in snake[1:]:
        return 1
    else:
        return 0



class Snake():
    def __init__(self):
        self.head=head
        self.snake = snake
        self.direction=4
    def move(self):
        global food
        if self.direction==1:
            self.head[1] -= 10
            self.snake.insert(0, list(self.head))
            if self.head != food:
                self.snake.pop(-1)
            else:
                food = [random.randrange(1, 50) * 10, random.randrange(1, 50) * 10]
        elif self.direction==2:
            self.head[1] += 10
            self.snake.insert(0, list(self.head))
            if self.head != food:
                self.snake.pop(-1)
            else:
                food = [random.randrange(1, 60) * 10, random.randrange(1, 60) * 10]
        elif self.direction==3:
            self.head[0] -= 10
            self.snake.insert(0, list(self.head))
            if self.head != food:
                self.snake.pop(-1)
            else:
                food = [random.randrange(1, 60) * 10, random.randrange(1, 60) * 10]
        elif self.direction==4:
            self.head[0] += 10
            self.snake.insert(0, list(self.head))
            if self.head != food:
                self.snake.pop(-1)
            else:
                food = [random.randrange(1, 60) * 10, random.randrange(1, 60) * 10]
        if hit_snake() or hit_wall(head):
            over()
    def display(self):
        for i in self.snake:
            pygame.draw.rect(windows, purple, pygame.Rect(i[0], i[1], 10, 10))
def start():
    image=pygame.image.load('D:/PycharmProjects/pythonProject/resources/snake2.png')
    windows.blit(image,(60,60))
    pygame.display.update()
    while True:  # 进行按键监听
        for event in pygame.event.get():  # 获取所有事件信息
            if event.type == pygame.QUIT:  # 如果用户鼠标点击了窗口的X
                pygame.quit()  # 终止程序
                exit()  # 终止程序
            elif event.type == pygame.KEYDOWN:  # 获取键盘按键信息
                if event.key == pygame.K_ESCAPE:  # 终止程序
                    pygame.quit()  # 终止程序
                else:
                    return 0  # 结束初始界面，开始玩蛇
def over():

    image = pygame.image.load('D:/PycharmProjects/pythonProject/resources/snake3.png')

    windows.blit(image, (50, 50))
    pygame.display.update()
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                exit()
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    pygame.quit()
                    exit()

''' def right_move(self):
        global food
        self.head[0]+=10
        self.snake.insert(0,list(self.head))
        if self.head != food:
            self.snake.pop(-1)
        else:
            food = [random.randrange(1, 60) * 10, random.randrange(1, 60) * 10]
    def up_move(self):
        global food
        self.head[1]-=10
        self.snake.insert(0,list(self.head))
        if self.head != food:
            self.snake.pop(-1)
        else:
            food = [random.randrange(1, 50) * 10, random.randrange(1, 50) * 10]
    def left_move(self):
        global food
        self.head[0]-=10
        self.snake.insert(0,list(self.head))
        if self.head != food:
            self.snake.pop(-1)
        else:
            food = [random.randrange(1, 60) * 10, random.randrange(1, 60) * 10]
    def down_move(self):
        global food
        self.head[1]+=10
        self.snake.insert(0,list(self.head))
        if self.head != food:
            self.snake.pop(-1)
        else:
            food = [random.randrange(1, 60) * 10, random.randrange(1, 60) * 10]'''
def key_control(self):
    # 获取事件，比如按键等
    for event in pygame.event.get():
        # 判断是否是点击了退出按钮
        if event.type == pygame.QUIT:
            print("exit")
            exit()
        if event.type == KEYDOWN:
            if event.key == pygame.K_ESCAPE:  # 如果按键ESC
                pygame.quit()
                exit()
            if self.direction!=2 and event.key == K_UP:
                print('up')
                self.direction=1
            elif self.direction !=1  and event.key == K_DOWN:
                print('down')
                self.direction = 2
            elif self.direction !=4  and event.key == K_LEFT:
                print('left')
                self.direction = 3
            elif self.direction !=3  and event.key == K_RIGHT:
                print('right')
                self.direction = 4
def main():
    window=pygame.display.set_mode((600,600))
    background=pygame.image.load('D:/PycharmProjects/pythonProject/resources/snake1.png')
    tittle=pygame.display.set_caption('贪吃蛇')
    food=Food()
    snake=Snake()
    start()
    while True:
        window.blit(background,(0,0))
        food.display()
        snake.display()
        key_control(snake)
        snake.move()
        pygame.display.update()
        speed.tick(30)
if __name__=='__main__':
    main()


